<template>
  <div>
    <!-- header 页头 -->
    <el-page-header
      content="添加用户"
      icon=""
      title="用户管理"
    />
    <el-card>
      <el-form
        ref="userFormRef"
        :model="userForm"
        :rules="userFormRules"
        label-width="120px"
        class="demo-ruleForm"
      >
        <el-form-item
          label="用户名"
          prop="username"
        >
          <el-input v-model="userForm.username" />
        </el-form-item>
        <el-form-item
          label="密码"
          prop="password"
        >
          <el-input
            v-model="userForm.password"
            type="password"
            autocomplete="off"
            show-password
          />
        </el-form-item>
        <el-form-item
          label="性别"
          prop="gender"
        >
          <el-select
            v-model="userForm.gender"
            class="m-2"
            placeholder="请选择"
            style="width:100%"
          >
            <el-option
              v-for="item in genderOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="角色"
          prop="role"
        >
          <el-select
            v-model="userForm.role"
            class="m-2"
            placeholder="请选择"
            style="width:100%"
          >
            <el-option
              v-for="item in roleOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="个人简介"
          prop="introduction"
        >
          <el-input
            v-model="userForm.introduction"
            type="textarea"
          />
        </el-form-item>
        <el-form-item
          label="头像"
          prop="avatar"
        >
          <UploadAvatar
            :avatar="userForm.avatar"
            @handle="handleUpload"
          ></UploadAvatar>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="submitForm()"
          >
            添加用户
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";
import axios from "axios";
import { ElMessage } from "element-plus";
import upload from '../../utils/upload'
import UploadAvatar from '../../components/upload/UploadAvatar.vue'
import { useRouter } from "vue-router";

const router = useRouter()
const userFormRef = ref();
const userForm = reactive({
  username: '',
  gender: '',
  introduction: '',
  role: 2,
  // avatar 和 file 都是代表的用户头像 avatar 用于前端用户回显，file 用户提交后端
  avatar: '',
  file: null
});

const userFormRules = reactive({
  username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
  gender: [{ required: true, message: "请选择性别", trigger: "blur" }],
  role: [{ required: true, message: "请选择角色", trigger: "blur" }],
  introduction: [{ required: true, message: "请输入介绍", trigger: "blur" }],
  avatar: [{ required: true, message: "请上传头像", trigger: "blur" }]
});
const genderOptions = [
  {
    label: "男",
    value: 1
  },
  {
    label: "女",
    value: 2
  }
];
const roleOptions = [
  {
    label: "管理员",
    value: 1
  },
  {
    label: "普通用户",
    value: 2
  }
];
// 每次选择完图片之后的回调
const handleUpload = (file) => {
  userForm.avatar = URL.createObjectURL(file);
  userForm.file = file;
}
const submitForm = () => {
  userFormRef.value.validate(async (valid) => {
    if (valid) {
      const res = await upload('/adminapi/user/add', userForm)
      if (res.ActionType === 'OK') {
        ElMessage.success('更新成功')
        router.push('/user-manage/userlist')
      }
    }
  });
}
</script>

<style lang="scss" scoped>
.el-card,
.el-form {
  margin-top: 20px;
}
</style>